package com.example.demo.admin.test.queue;/**
 * Created by kete on 2018-09-25.
 */

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 作者：kete
 * 创建时间：2018-09-25 16:08
 * 功能描述：
 * 版本：
 */
public class ConsumerFile implements Runnable {

    private final BlockingQueue<File> queue;

    public ConsumerFile(BlockingQueue<File> queue) {
        this.queue = queue;
    }

    public static void main(String[] args) {
        BlockingQueue<File> queue = new LinkedBlockingQueue<>();

        File file = new File("H:\\文档");
        File[] roots = file.listFiles();

        for(File a: roots)
            new Thread(new ProducerFile(queue,a)).start();

        for(int i=0;i<3;i++)
            new Thread(new ConsumerFile(queue)).start();
    }

    @Override
    public void run() {
        while (true){
            try {
                indexFile(queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }

    private void indexFile(File file){
        String fileName = file.getName();

        if(fileName.indexOf("java")>=0||fileName.indexOf("JAVA")>=0){
            System.out.format("current name {%s},file name {%s}",Thread.currentThread().getName(),fileName);
            System.out.printf("\n");
        }
    }
}
